<template>
  <view class="scanBingDing">
    <detailsOfThePowerPlant
      :isDisabled="true"
      :dataList="dataList"
      :isShowBindType="true"
    />
    <view class="bottom-btn">
      <up-button
        type="primary"
        class="btn-submit"
        :text="t('dashboard.bind')"
        shape="circle"
        :disabled="!dataList[0].bind"
        @click="handleBingDing"
      ></up-button>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
const dataList = ref([]);
import { onLoad } from "@dcloudio/uni-app";
import apis from "@/apis/index.js";
import { useI18n } from "vue-i18n";
const { t } = useI18n();

onLoad((options) => {
  dataList.value.push(JSON.parse(options.data));
  console.log("dataList.value", dataList.value);
});

const handleBingDing = () => {
  if (dataList.value[0].bind) {
    apis.bindTheSite({ siteId: dataList.value[0].sid }).then((res) => {
      if (res.code == 200) {
        if (res.data == 1) {
          uni.showToast({
            title: t("dashboard.theBindingIsSuccessful"),
            icon: "none",
          });
          let params = {
            emsCode: dataList.value[0].emsCode,
          };
          apis.addSearchSite(params).then((res) => {
            if (res.code == 200) {
              dataList.value = [];
              dataList.value.push(res.data);
            }
          });
        } else {
          uni.showToast({
            title: t("dashboard.bindingFailed"),
            icon: "none",
          });
        }
      }
    });
  }
};
</script>

<style lang="scss" scoped>
.scanBingDing {
  width: 100%;
  .bottom-btn {
    width: 100%;
    padding: 20rpx 56rpx 56rpx 56rpx;
    position: fixed;
    bottom: 0;
    left: 0;
    box-sizing: border-box;
    z-index: 999;
    background-color: #08121e;
  }
}
</style>
